package com.crm.report.controller;

import com.crm.custinfo.service.impl.UserInfoComm;
import com.crm.report.service.ICustStaticsService;
import com.crm.report.service.IUserCostStaticsService;
import com.ruoyi.common.constant.TypeConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 功能说明: 用户成本统计
 * 系统版本: v1.0
 * 开发人员: @author Mr.Wen
 * 开发时间: 2024-02-28
 */
@RestController
@RequestMapping("/crmsys/userCostStatics")
public class UserCostStaticsController extends BaseController {
    @Autowired
    private IUserCostStaticsService iUserCostStaticsService;
    @Autowired
    private ISysDeptService isysDeptService;
    @Autowired
    private UserInfoComm userInfoComm;
    @Autowired
    private ISysUserService userService;
    /**
     * 用户成本统计查询
     */
    @GetMapping("/custdebtlimitdataStatics/{timeGroup}/{deptId}")
    public TableDataInfo custdebtlimitdataStatics(@PathVariable String timeGroup, @PathVariable Long deptId)
    {
        //获取登录账号的角色信息
        Map<String, Object> roleMap = userInfoComm.getUserRoleCode();

        Map<String,Object> params = new HashMap<>();
        SysDept sysDept=getLoginUser().getUser().getDept();
        if (Objects.equals(sysDept.getLevel(),String.valueOf(TypeConstant.ORG_LEVEL_4))) {
            if (MapUtils.getString(roleMap, "depart_leader", "0").equals("0")) { // 业务员
                // 业务员只能看到本人的日志
                params.put("empId", getUserId());
            }
        }
        if(deptId!=-1L){
            sysDept=new SysDept();
            sysDept.setDeptId(deptId);
        }

        Long[] orgIds=getSysDepts(sysDept);
        params.put("orgIds", orgIds);
        if(timeGroup != null&& !timeGroup.isEmpty()) {
            String[] time = timeGroup.split(",");
            params.put("startTime", time[0]);
            params.put("endTime", time[1]+" 23:59:59");
        }

        List<Map<String, Object>> list =iUserCostStaticsService.userCostStatics(params);

        return getDataTable(list);
    }

    /**
     * 个人数据统计
     * @param user
     * @return
     */
    @GetMapping("/getUserReport")
    public AjaxResult getUserReport(SysUser user)
    {
        Map<String,Object> resultMap=new HashMap<>();
        List<Map<String, Object>> list = userService.getUserArrangement(user);
        Map<String,Object> inParams=new HashMap<>();
        inParams.put("userId",user.getUserId());
        List<Map<String, Object>> list2 = iUserCostStaticsService.userDataStatics(inParams);
        resultMap.put("resMap",list);
        resultMap.put("dataStaticMap",list2);

        return AjaxResult.success(resultMap);
    }

    /**
     * 获取权限部门的ids
     * @param sysDept
     * @return
     */
    private Long[] getSysDepts(SysDept sysDept) {
        List<SysDept> sysDepts = isysDeptService.selectChildrenAllDeptById(sysDept.getDeptId());
        Long[] orgIds = new Long[sysDepts.size()];
        for (int i = 0; i < sysDepts.size(); i++) {
            SysDept sysDept2 = sysDepts.get(i);
            orgIds[i] = sysDept2.getDeptId();
        }
        return orgIds;
    }


}
